import { Component, OnInit } from '@angular/core';
import { HeroService } from '../../../../services/hero.service';
import { StorageService } from '../../../../services/storage.service';
import { NzMessageService,NzModalService, UploadFile, UploadFilter } from 'ng-zorro-antd';
import { options } from '../../../../common/areaoption';
import { LogoimgSyncService, LogoimgSync } from '../../../../services/logoimg-async'

@Component({
  selector: 'app-editpersoninfo',
  templateUrl: './editpersoninfo.component.html',
  styleUrls: ['./editpersoninfo.component.css']
})

export class EditpersoninfoComponent implements OnInit {

  constructor(private heroService: HeroService,
              private storageService: StorageService,
              private logoimgSyncService: LogoimgSyncService,
              private message: NzMessageService,
              private modalService: NzModalService) {}

  isLoading=false;
  btn_loading=false;
  public type: number;
  public name: string;
  public tel: string;
  public logoUrl: string;
  public logocode: number;
  public remark: string='';
  public telcode: number;
  public selectArea: number = -2;
  public companyLogo = host + "/plupload";
  public nzOptions = options;
  public values: Array<any> = [];//点击选择触发的values
  public values2: Array<any> = [];//回显遍历的values2
  public values3: Array<any> = [];//最终提交修改的 values3
  public lables: Array<any> = [];
  public fileList = [];
  public onChanges(values: any): void {
    this.values = values;
    if (this.values[1]) {
      this.selectArea = 0;
    }
    if (this.values.length == 0) {
      this.selectArea = -1;
    }
  }

  ngOnInit(): void {
    // 回显个人资料
    this.backView();
  }

  //提交资料更改
  editInfo() {
    if(this.selectArea == -2){
      this.values3 = this.values2;
    }else{
      if(this.selectArea == 0){
        this.values3=this.values;
        if(!this.values3[2]){
          this.values3[2] = "";
        }
      }
    }
    if(!this.tel){
      this.telcode = -1;
    }
    if(!this.logoUrl){
      this.logocode = -1;
    }
    if(this.selectArea == -1 || this.telcode == -1 || this.logocode == -1){
      return false;
    }else{
      // console.log(this.values);
      // console.log(this.values2);
      // console.log(this.values3);
      this.type = this.storageService.getItem('type');
      if(this.type == 1){
        this.btn_loading=true;
        let url = host + "/main/merchant/edit";
        let model ='tel='+this.tel+'&provinceId='+this.values3[0]+'&cityId='+this.values3[1]
          +'&districtId='+this.values3[2]+'&logo='+this.logoUrl+'&remark='+this.remark;
        this.heroService.post(url, model).then(result => {
          this.btn_loading=false;
          if(result.code == 0){
            this.message.success('更改资料成功');
            this.selectArea = -2;
            this.backView();
          }else{
            this.modalService.error({
              nzTitle: '提示',
              nzContent: result.msg
            });
          }
        })
      }else{
        this.modalService.error({
          nzTitle: '提示',
          nzContent: '只有超管账号才可以修改!'
        });
        // this.message.error('只有超管账号才可以修改!');
      }
    }
  }

  // 回显个人资料
  backView() {
    let url = host + "/main/merchant/view";
    this.heroService.get(url).then(result => {
      if(result.code == 0){
        this.name = result.data.bean.name;
        this.tel = result.data.bean.tel;
        if (result.data.bean.logo) {
          this.logoUrl = result.data.bean.logo;
          this.logoimgSyncService.publishInfo({
            url: this.logoUrl
          });
        }
        this.remark = result.data.bean.remark?result.data.bean.remark:'';
        this.values2 = [result.data.bean.provinceId, result.data.bean.cityId, result.data.bean.districtId];
        // console.log(this.values2);
        for (var i in this.nzOptions) {
          if (this.nzOptions[i].value == this.values2[0]) {
            this.lables[0] = this.nzOptions[i].label;
            for (var j in this.nzOptions[i].children) {
              if (this.nzOptions[i].children[j].value == this.values2[1]) {
                this.lables[1] = this.nzOptions[i].children[j].label;
                if (this.values2[2]) {
                  for (var k in this.nzOptions[i].children[j].children) {
                    if (this.nzOptions[i].children[j].children[k].value == this.values2[2]) {
                      this.lables[2] = this.nzOptions[i].children[j].children[k].label;
                    }
                  }
                }
              }
            }
          }
        }
        // console.log(this.lables);
        this.values = this.lables;
        // console.log(this.values);
      }else{
        this.modalService.error({
          nzTitle: '提示',
          nzContent: result.msg
        });
      }
    })
  }
  // 筛选判断上传图片格式
  filters: UploadFilter[] = [
    {
      name: 'type',
      fn: (fileList: UploadFile[]) => {
        const filterFiles = fileList.filter(w => ~['image/png', 'image/jpeg', 'image/gif', 'image/bmp'].indexOf(w.type));
        if (filterFiles.length !== fileList.length) {
          this.message.error(`只能上传图片`);
          return filterFiles;
        }
        return fileList;
      }
    }
  ];
  // 获取上传logo图片返回信息
  logoChange(info: any): void {
    this.isLoading = true;
    this.logocode = 0;
    if (info.type == "success") {
      info.file.url = info.file.response.url;
      this.logoUrl = info.file.url;
      this.isLoading = false;
      this.message.success('上传成功,点击更改基本信息确认');
    }
    if (info.type == "removed") {
      this.message.warning('删除成功');
    }
    // if(info.fileList.length>1){
    //   this.message.error('只能上传一张图片！'); 
    //   info.fileList.splice(1,1);
    // }
  }
  telFocus() {
    this.telcode = 0;
  }
}
